﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Cashier_Service;
using Cashier.utils;
using Cashier_Model;

namespace Cashier.report
{
    public partial class UserShipmentReportControl : UserControl
    {
        private MainForm mainform;
        private static UserShipmentReportControl userShipmentReportControl;
        private ReportService reportService;
        private AdminService adminService;

        /// <summary>
        /// 构造函数
        /// </summary>
        public UserShipmentReportControl(MainForm mainform)
        {
            InitializeComponent();
            this.mainform = mainform;
            this.reportService = new ReportService();
            this.adminService = new AdminService();
            mainform.SetPageInfo(0, 0, 0);

            this.InitialData();
            this.Dock = DockStyle.Fill;
        }

        /// <summary>
        /// 获取UserShipmentReportControl实例
        /// </summary>
        /// <param name="mainform"></param>
        /// <returns></returns>
        public static UserShipmentReportControl GetUserShipmentReportControl(MainForm mainform)
        {
            if (userShipmentReportControl == null)
            {
                userShipmentReportControl = new UserShipmentReportControl(mainform);
            }

            return userShipmentReportControl;
        }


        #region 功能方法

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitialData()
        {
            try
            {
                DataTable dt_years = this.reportService.GetShipmentReportAllYears();
                if (dt_years != null && dt_years.Rows.Count == 0)
                {
                    dt_years.Rows.Add(DateTime.Now.Year);
                }
                comboBox_year.DataSource = dt_years;
                comboBox_year.DisplayMember = "years";
                comboBox_year.ValueMember = "years";
                comboBox_year.Text = DateTime.Now.Year.ToString();

                /*
                for (int i = 1; i < 13; i++)
                {
                    comboBox_month.Items.Add(DataItem.GetDataItem(i.ToString(), i.ToString()));
                }
                comboBox_month.DisplayMember = "displayMember";
                comboBox_month.ValueMember = "valueMember";
                comboBox_month.SelectedIndex = DateTime.Now.Month - 1;
                 * */

                DataTable dt_user = adminService.GetAdminTable(0, 0, mainform.Admin.EnterpriseNo, mainform.Admin.StoreNo, 0);
                comboBox_user.DataSource = dt_user;
                comboBox_user.DisplayMember = "name";
                comboBox_user.ValueMember = "username";
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        #endregion

        #region 控件事件

        /// <summary>
        /// 生成报表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_report_Click(object sender, EventArgs e)
        {
            try
            {
                if (comboBox_year.SelectedValue != null && comboBox_user.SelectedValue!=null)
                {
                    int years = Convert.ToInt32(comboBox_year.SelectedValue);
                    ReportParams reportParams = new ReportParams();
                    reportParams.Years = years;
                    reportParams.Username = comboBox_user.SelectedValue.ToString();
                    reportParams.Groupfield = "months";
                    reportParams.Orderfield = "months";
                    DataTable dt = this.reportService.ReportBillShipment(reportParams);
                    MSChartUtil.UserShipmentChartReport(chart1, dt);
                }
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        #endregion
    }
}
